package src.greedy_algorithms;

import java.util.Arrays;
import java.util.PriorityQueue;

/**
 * @author starsea
 * @date 2024-08-25 18:54
 */

public class Test02 {
    public static void main(String[] args) {
        int[] g = new int[]{1, 2, 3};
        int[] s = new int[]{1, 1};
        System.out.println(findContentChildren(g, s));
        int[] players = new int[]{4, 7, 9};
        int[] trainers = new int[]{8, 2, 5, 8};
        System.out.println(matchPlayersAndTrainers(players, trainers));
    }

    public static int matchPlayersAndTrainers(int[] players, int[] trainers) {
        Arrays.sort(players);
        Arrays.sort(trainers);
        PriorityQueue<Integer> q = new PriorityQueue<>((a, b) -> {
            return a-b;
        });
        for (int i = 0; i < trainers.length; i++) {
            q.offer(trainers[i]);
        }
        int ans = 0;
        int i = 0;
        while (i < players.length && !q.isEmpty()) {
            while( !q.isEmpty() && q.peek()<players[i])
            {
                q.poll();
            }
            if(q.isEmpty())
            {
                break;
            }
            ans++;
            i++;
            q.poll();
        }
        return ans;

    }

    public static int findContentChildren(int[] g, int[] s) {
        Arrays.sort(g);
        Arrays.sort(s);

        int i = 0;
        int j = 0;
        int ans = 0;
        while (i < g.length && j < s.length) {
            while (j < s.length && g[i] > s[j]) {
                j++;
            }
            if (j >= s.length) {
                break;
            }
            ans++;
            i++;
            j++;
        }
        return ans;

    }
}
